/*
 * QTXpost - a Qt-based blog client.
 * Copyright (C) 2010 Helen Sommer
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program (in COPYING); if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA  02111-1307  USA
 *
 * The project is hosted on http://code.google.com/p/qtxpost
 */

#include "PostProgress.h"

PostProgress::PostProgress(QWidget *parent) 
  : QDialog(parent)
{
  setupUi(this);

  // ugly hack because of textBrowser bug: after clicking any link, all subsequent text is rendered as links, even after clear() and replacing with other text. WebView doesn't have this problem but it doesn't allow appending text. So we use textView (without link clicking) while the output is still updating, and silently replace it with webView when the update is finished.
  // also, webView can't be added through Qt Designer, as it's not in the list of widgets, so it's added here.

  webView = new QWebView(this);
  verticalLayout->addWidget(webView);
  webView->hide();
  labelWebView->hide();
  QWebPage *page = webView->page();
  page->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);

  textBrowser->setOpenLinks(false);
  textBrowser->setOpenExternalLinks(false);

  //connect(textBrowser, SIGNAL(anchorClicked(const QUrl &)), this, SLOT(linkView(const QUrl &)));
  connect(webView, SIGNAL(linkClicked(const QUrl &)), this, SLOT(linkView(const QUrl &)));

  connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
}

void PostProgress::initialize(int target_, bool doClear)
{

  waitLabel->show();
  if (doClear) {
    /*
      // it doesn't work at the end; just delete it and recreate
    textBrowser->clear();
    webView->hide();
    textBrowser->show();
    */
    webView->hide();
    delete textBrowser;
    textBrowser = new QTextBrowser();
    verticalLayout->addWidget(textBrowser);
  }
  progressBar->setValue(0);
  target = target_;
  count = 0;
  step = 100/target;
}

void PostProgress::quit()
{
  waitLabel->hide();
  progressBar->setValue(100);
  emit finished();
  
  QString content = textBrowser->toHtml();
  // qDebug() << content;
  textBrowser->hide();
  webView->setHtml(content);
  webView->show();
}

void PostProgress::ok()
{
  count++;
  progressBar->setValue(progressBar->value() + step);
  if (count >= target) 
    quit();
}

void PostProgress::update(QString str)
{
  textBrowser->append(str);
}

void PostProgress::updateUrl(QString str)
{
  // QString url = QString("<a href=\"").append(str).append("\">").append(str).append("</a>");
  QString url = QString("<a href=\"%1\">%2</a>").arg(str).arg(str);
  textBrowser->append(url);
}

void PostProgress::linkView(const QUrl & url)
{
  QDesktopServices::openUrl(url);
}
